package com.example.cdctest.controller;

import com.example.cdctest.pojo.UserInfo;
import com.example.cdctest.service.KnowledgeCollectionService;
import com.example.cdctest.service.KnowledgeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/services")
public class KnowledgeCollectionController {
    @Resource
    private KnowledgeCollectionService knowledgeCollectionService;
    @RequestMapping("addCollection")
    @ResponseBody
    public String addCollection(int knowledgeId, HttpSession session){
        UserInfo loginUser =(UserInfo) session.getAttribute("loginUser");
        if(loginUser==null){
            return "1";
        }else{
           Boolean isCollection= knowledgeCollectionService.isCollection(loginUser.getId(),knowledgeId);
            if(isCollection){
                knowledgeCollectionService.deleteCollection(loginUser.getId(),knowledgeId);
                return "0";
            }else {
                knowledgeCollectionService.collectionKnowledge(loginUser.getId(), knowledgeId);
                return "0";
            }
        }

    }
}
